/**
 * Created by Xiaoming on 2017/8/29.
 */
angular.module('module.auth')
    .controller('RepasswordController', RepasswordController);

RepasswordController.$inject = ['AuthService', 'AuthUserService'];
function RepasswordController(AuthService, AuthUserService) {
    var vm = this;
    vm.user = {};
    vm.password = {};
    vm.getcode = getcode;
    vm.retrieve = retrieve;
    //获取修改密码验证码
    function getcode(user) {
        if (!judgePhone(user.phone)) {
            $(".tel").focus();
            $(".tel").select();
            return;
        }
        settime();
        AuthService.getcode(user.phone,'rePassword', function () {
        }, function (reason) {
            if(reason.data){
                settime(0);
                input_alert(reason.data.msg);
            }else{
                input_alert("发送短信失败，请重试");
            }
        });
    }
    //提交
    function retrieve(user) {
        if (!judgePhone(user.phone)) {
            $(".tel").focus();
            $(".tel").select();
            return;
        }
        if (!judgePass(user.newPassword)) {
            $(".psw").focus();
            $(".psw").select();
            return;
        }
        if (!user.code || user.code == "") {
            input_alert("请输入验证码");
            return;
        }
        AuthService.retrieve(vm.user,function (data) {
            AuthUserService.addUserToCookies(data);
            if(data.result){
                input_alert(data.result, 2000);
            }else{
                input_alert('密码重置成功', 2000);
            }
            setTimeout(function () {
                window.history.back();
            }, 2000);
        }, function (data) {
            if(data.status){

            }
            if (data.status == 405 || data.status == 404) {
                input_alert("系统繁忙,请稍后再试", 1000);
            } else {
                input_alert(data.data.reason, 1000);
            }
        })
    }
}